package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.handler.SubjectTypeHandler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SubjectTypeHandlerFactory implements InitializingBean {
    @Resource
    public List<SubjectTypeHandler> subjectTypeHandlerList;

    public Map<SubjectInfoTypeEnum,SubjectTypeHandler> handlerMap = new HashMap<>();


    public SubjectTypeHandler getHandler(int subjectType) {
        SubjectInfoTypeEnum typeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
        return handlerMap.get(typeEnum);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        subjectTypeHandlerList.forEach(typeHandler->{
            handlerMap.put(typeHandler.getHandlerType(),typeHandler);
        });
    }
}